package xy.donate.android.ui.fragment;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.base.BaseAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import xy.donate.android.R;
import xy.donate.android.action.StatusAction;
import xy.donate.android.common.MyFragment;
import xy.donate.android.constant.Constant;
import xy.donate.android.db.OrderDao;
import xy.donate.android.db.OrderDb;
import xy.donate.android.entity.OrderBean;
import xy.donate.android.helper.MmkvHelper;
import xy.donate.android.ui.activity.OrderDetailActivity;
import xy.donate.android.ui.activity.OrderListActivity;
import xy.donate.android.ui.adapter.OrderAdapter;
import xy.donate.android.widget.HintLayout;

public class OrderFragment extends MyFragment<OrderListActivity> implements StatusAction, OnItemChildClickListener {

    public int index = 0;
    private OrderAdapter adapter;

    public static OrderFragment getInstance(int index){
        OrderFragment fragment = new OrderFragment();
        fragment.index = index;
        return fragment;
    }

    private RecyclerView recyclerView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_list;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        adapter = new OrderAdapter(R.layout.item_order_list);
        adapter.setOnItemChildClickListener(this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        OrderDao dao = Room.databaseBuilder(requireContext(), OrderDb.class,"orderDB")
                .allowMainThreadQueries()
                .build().orderDao();
        List<OrderBean> list = dao.getList(MmkvHelper.getInstance().getMmkv().decodeString(Constant.USER_ID_NUM,""),index);
        if (list != null && list.size() != 0){
            getHintLayout().setVisibility(View.GONE);
            adapter.setList(list);
        }else {
            showEmpty();
        }
    }

    @Override
    public HintLayout getHintLayout() {
        return findViewById(R.id.hint_layout);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter1, @NonNull View view, int position) {
        Intent intent = new Intent(getContext(), OrderDetailActivity.class);
        intent.putExtra("OrderNo",adapter.getItem(position).getOrderNo());
        startActivity(intent);
    }
}
